package operation.command.obj;

import operation.command.abs.ICommand;

/**
 * Invoker
 */
public class App {
    // 聚合命令
    private ICommand[] onCommands;
    private ICommand[] offCommands;

    private ICommand undoCommand;

    public App() {
        onCommands = new ICommand[5];
        offCommands = new ICommand[5];
        for (int i = 0; i < 5; i++) { // 全部初始为空命令
            onCommands[i] = new NullCommand();
            offCommands[i] = new NullCommand();
        }
    }

    public void setCommand(int id, ICommand on, ICommand off) {
        onCommands[id] = on;
        offCommands[id] = off;
    }

    public void pressOn(int id) {
        (undoCommand = onCommands[id]).execute();
    }

    public void pressOff(int id) {
        (undoCommand = offCommands[id]).execute();
    }

    public void pressUndo() {
        undoCommand.undo();
    }
}